import { isObject } from "@/utils/judgment";

/**
 * 深度合并(非链表)
 * 通过递归, 实现对象的深度合并
 * @param src 源对象
 * @param target 目标对象
 */
const deepMerge = (src: any, target: any) => {
  let key: string;
  for (key in target) {
    src[key] = isObject(src[key])
      ? deepMerge(src[key], target[key])
      : (src[key] = target[key]);
  }
  return src;
};

/**
 * 深度合并(链表)
 * 通过递归, 实现对象的深度合并
 * @param src 源对象
 * @param target 目标对象
 */
const deepMergeLinkList = (src: any, target: any) => {
  target.forEach((item: any, index: number) => {
    src[index] = isObject(src[index])
      ? deepMergeLinkList(src[index], item)
      : (src[index] = item);
  });
  return src;
};

export { deepMerge, deepMergeLinkList };
